package com.dayuanit.dy8.model.sigle.proxy.dynamic;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {

    private Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {

        System.out.println("XXXXXXXXXXXXX" + method.getName());
        try {

            Annotation[] anns = method.getAnnotations();
            boolean autoCommit = true;

            for (Annotation ann : anns) {
                if (ann instanceof MyTrans) {
                    autoCommit = false;
                }
            }

            if (!autoCommit) {
                System.out.println("befor saveUser");
                method.invoke(target, args);
                System.out.println("after saveUser");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
